package com.wx.mall.operate.controller;


import com.alibaba.fastjson.JSON;
import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.operate.dto.RebBrandTwoDto;
import com.wx.mall.operate.dto.VBrandsDto;
import com.wx.mall.operate.entity.VBrands;
import com.wx.mall.operate.service.impl.VBrandsServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.est.CACertsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/v/brands")
public class VBrandsController {

    @Autowired
    VBrandsServiceImpl vBrandsService;

    @ApiOperation(value = "根据id已选择品牌")
    @GetMapping("/getFindPost")
    public CommonResult<CommonPage<VBrands>> findBrandSelect(VBrandsDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<VBrands> finds = vBrandsService.findBrandSelect(dto);
        return CommonResult.success(CommonPage.restPage(finds));
    }

    @ApiOperation(value = "筛选分页查询")
    @GetMapping("/getBrandScreen")
    public CommonResult<CommonPage<VBrands>> findBrandScreen(RebBrandTwoDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<VBrands> brandScreen = vBrandsService.findBrandScreen(dto);
        return CommonResult.success(CommonPage.restPage(brandScreen));
    }

    @ApiOperation(value = "品牌修改")
    @PostMapping("/getFindBraUpdate")
    public CommonResult<VBrands> getFindBraUpdate(Long ids,String vBrands){
        List<VBrands> vBrands1 = JSON.parseArray(vBrands, VBrands.class);
        int vBraUpdate = vBrandsService.findVBraUpdate(ids, vBrands1);
        return vBraUpdate==1?CommonResult.success():CommonResult.failed();
    }
}

